package com.tgpms.web.element.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgpms.web.element.dao.WsElementRecordDao;
import com.tgpms.web.element.dto.WsElementRecordDto;
import com.tgpms.web.element.entity.WsElementRecord;
import com.tgpms.web.element.service.WsElementRecordService;
import com.tgpms.web.element.vo.WsElementRecordVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("wsElementRecordService")
public class WsElementRecordServiceImpl extends ServiceImpl<WsElementRecordDao, WsElementRecord> implements WsElementRecordService {

    @Override
    public List<WsElementRecordDto> getByCommodityNumber(String commodityNumber, String examination) {
        return this.baseMapper.getByCommodityNumber(commodityNumber, examination);
    }

    @Override
    @Transactional
    public void saveWsElementRecord(WsElementRecordVo wsElementRecordVo) {
        String commodityNumber = wsElementRecordVo.getCommodityNumber();
        String examination = wsElementRecordVo.getExamination();
        this.removeByCommodityNumber(commodityNumber, examination);
        List<WsElementRecord> wsElementRecords = wsElementRecordVo.getList().stream().map(e -> {
            WsElementRecord wsElementRecord = new WsElementRecord();
            wsElementRecord.setExamination(examination);
            wsElementRecord.setCommodityNumber(commodityNumber);
            wsElementRecord.setWseCode(e);
            wsElementRecord.setStatus("Y");
            wsElementRecord.setInsertUser("TGPMS");
            wsElementRecord.setInsertDate(new Date());
            wsElementRecord.setDataOwner("BHTPMS");
            return wsElementRecord;
        }).collect(Collectors.toList());
        this.saveBatch(wsElementRecords);
    }

    @Override
    public void removeByCommodityNumber(String commodityNumber, String examination) {
        this.remove(new LambdaQueryWrapper<WsElementRecord>().eq(WsElementRecord::getCommodityNumber, commodityNumber).eq(WsElementRecord::getExamination, examination));
    }

}